document.write('\<script type=\"text\/javascript\" src=\"js\/xml.js\"\>\<\/script\>');
document.write('\<script type=\"text\/javascript\" src=\"js\/rgui.js\"\>\<\/script\>');

var namexhr = false;
var emailxhr = false;

var nameok = false;
var emailok = false;

function checkUsername(inputField) {
	var name = inputField.value;
	var msgspan = document.getElementById('usernamemsg');
	emptyNode(msgspan);
	if (name == '') {
		//用户名不能为空
		msgspan.appendChild(document.createTextNode('\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a'));
		msgspan.style.color = 'red';
		nameok = false;
		return;
	}
	namexhr = getXMLRequest();
	checkAvaiable(namexhr, 'checkUsernameAvaiable', 'username', name, usernameCallback);
}

function usernameCallback() {
	var msgspan = document.getElementById('usernamemsg');
	emptyNode(msgspan);
	if(namexhr.readyState == 1) {
		//正在检查用户名是否可用……
		msgspan.appendChild(document.createTextNode('\u6b63\u5728\u68c0\u67e5\u7528\u6237\u540d\u662f\u5426\u53ef\u7528\u2026\u2026'));
	}
	if (namexhr.readyState == 4) {
		if (namexhr.status == 200) {
			var json = jsonParse(namexhr.responseText);
			if (json.errcode != '0') {
				msgspan.style.color = 'red';
				nameok = false;
			} else {
				msgspan.style.color = 'green';
				nameok = true;
			}
			msgspan.appendChild(document.createTextNode(json.errmsg));
		}
	}
}

function checkAvaiable(xhr, type, key, value, callback) {
	if (xhr) {
		var url = document.URL.split('reg.jsp')[0] + 'user.do?action=' + type + '&' + key + '=' + value;
		xhr.onreadystatechange = callback;
		xhr.open('GET', url, true);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		xhr.send(null);
	}
}

function showPSField() {
	document.getElementById('psfield').style.visibility = 'visible';
}

function checkEmail(inputField) {
	var email = inputField.value;
	var msgspan = document.getElementById('emailmsg');
	emptyNode(msgspan);
	if (email == '') {
		//电子邮件地址不能为空
		msgspan.appendChild(document.createTextNode('\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u4e0d\u80fd\u4e3a\u7a7a'));
		msgspan.style.color = 'red';
		emailok = false;
		return;
	}
	var myReg = /^([-_A-Za-z0-9\.]+)@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
	if (!myReg.test(email)) {
		//请输入正确的电子邮件地址
		msgspan.appendChild(document.createTextNode('\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740'));
		msgspan.style.color = 'red';
		emailok = false;
		return;
	}
	emailxhr = getXMLRequest();
	checkAvaiable(emailxhr, 'checkEmailAvaiable', 'email', email, emailCallback);
}

function emailCallback() {
	var msgspan = document.getElementById('emailmsg');
	if(emailxhr.readyState == 1) {
		//正在检查电子邮件地址是否可用……
		msgspan.appendChild(document.createTextNode('\u6b63\u5728\u68c0\u67e5\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u662f\u5426\u53ef\u7528\u2026\u2026'));
	}
	if (emailxhr.readyState == 4) {
		emptyNode(msgspan);
		if (emailxhr.status == 200) {
			var json = jsonParse(emailxhr.responseText);
			if (json.errcode != '0') {
				msgspan.style.color = 'red';
				emailok = false;
			} else {
				msgspan.style.color = 'green';
				emailok = true;
			}
			msgspan.appendChild(document.createTextNode(json.errmsg));
		}
	}
}

function checkPassword(inputFiled) {
	var cpwd = inputFiled.value;
	var pwd = document.getElementById('password').value;
	var msgspan = document.getElementById('psmsg');
	emptyNode(msgspan);
	if (pwd != cpwd) {
		msgspan.style.color = 'red';
		//两次输入的密码不一致
		msgspan.appendChild(document.createTextNode('\u4e24\u6b21\u8f93\u5165\u7684\u5bc6\u7801\u4e0d\u4e00\u81f4'));
		return;
	}
}

function checkRegister() {
	if (!nameok) {
		// 请输入合法的用户名
		createAlertWindow('\u8bf7\u8f93\u5165\u5408\u6cd5\u7684\u7528\u6237\u540d', '\u9519\u8bef', '#F2543A', false);
		return false;
	}
	if (!emailok) {
		// 请输入合法的电子邮件地址
		createAlertWindow('\u8bf7\u8f93\u5165\u5408\u6cd5\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740', '\u9519\u8bef', '#F2543A', false);
		return false;
	}
	var pwd = document.getElementById('password').value;
	var cpwd = document.getElementById('repassword').value;
	if (pwd == '') {
		// 请输入密码
		createAlertWindow('\u8bf7\u8f93\u5165\u5bc6\u7801', '\u9519\u8bef', '#F2543A', false);
		return false;
	}
	if (cpwd == '') {
		// 请确认密码
		createAlertWindow('\u8bf7\u786e\u8ba4\u5bc6\u7801', '\u9519\u8bef', '#F2543A', false);
		return false;
	}
	if (pwd != cpwd) {
		// 两次输入的密码不一致
		createAlertWindow('\u4e24\u6b21\u8f93\u5165\u7684\u5bc6\u7801\u4e0d\u4e00\u81f4', '\u9519\u8bef', '#F2543A', false);
		return false;
	}
	var capt = document.getElementById('captchacode').value;
	if (capt == '' || capt.length != 4) {
		// 请输入验证码
		createAlertWindow('\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801', '\u9519\u8bef', '#F2543A', false);
		return false;
	}
	if (!document.getElementById('agree').checked) {
		// 你必须同意许可协议才能注册
		createAlertWindow('\u4f60\u5fc5\u987b\u540c\u610f\u8bb8\u53ef\u534f\u8bae\u624d\u80fd\u6ce8\u518c', '\u9519\u8bef', '#F2543A', false);
		return false;
	}
	return true;
}

function refreshCaptcha() {
	document.getElementById('captcha').src = 'code.jpg?' + Math.random();
}
